package com.binfenhulian.chinaenergy.ces.manage.controller.content;

import com.binfenhulian.chinaenergy.ces.core.bean.enums.ExceptionDescEnum;
import com.binfenhulian.chinaenergy.ces.core.exception.BaseException;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.ContentParam;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.SessionParam;
import com.binfenhulian.chinaenergy.ces.core.bean.result.AjaxResult;
import com.binfenhulian.chinaenergy.ces.manage.controller.base.BaseController;
import com.binfenhulian.chinaenergy.ces.manage.framework.interceptor.ManageLoginCheck;
import com.binfenhulian.chinaenergy.ces.manage.service.account.OnlineService;
import com.binfenhulian.chinaenergy.ces.manage.service.content.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by wanghjbuf on 2018/3/20.
 */
@ManageLoginCheck
@Slf4j
@Controller
@RequestMapping("/manage/readedit")
public class ReadEditController extends BaseController {

    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.service.content.ContentServiceImpl")
    ContentService contentService;
    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.service.account.OnlineServiceImpl")
    OnlineService onlineService;

    @RequestMapping("/index.html")
    public ModelAndView listContent(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("manage/readedit", getParamMap(request));
    }


    @RequestMapping("/details")
    @ResponseBody
    public Object details(HttpServletRequest request, HttpServletResponse response, String recordNo) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ContentParam result = contentService.detailByRecordNo(recordNo);
            ajaxResult = new AjaxResult(0, "查找成功", true, result);
        } catch (BaseException e){
            log.error(e.getMessage(), e);
            ajaxResult = new AjaxResult(0, e.getMessage(), false, null);
        } catch (Exception e) {
            log.error(ExceptionDescEnum.BASE.getExceptionDesc(), e);
            ajaxResult = new AjaxResult(0, ExceptionDescEnum.BASE.getExceptionDesc(), false, null);
        }
        return ajaxResult;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(HttpServletRequest request, HttpServletResponse response, @Valid ContentParam contentParam, BindingResult bindingResult) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            getVaildErrors(bindingResult);
            SessionParam sessionParam = onlineService.getOnlineUserByRequest(request);
            int count = contentService.save(contentParam, sessionParam);
            ajaxResult = new AjaxResult(0, "查找成功", true, count);
        } catch (BaseException e){
            log.error(e.getMessage(), e);
            ajaxResult = new AjaxResult(0, e.getMessage(), false, null);
        } catch (Exception e) {
            log.error(ExceptionDescEnum.BASE.getExceptionDesc(), e);
            ajaxResult = new AjaxResult(0, ExceptionDescEnum.BASE.getExceptionDesc(), false, null);
        }
        return ajaxResult;
    }
}
